/*
 * TowerGreenTwo.cs
 *
 * Created on December 16, 2007, 7:22 PM
 *
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * Copyright (C) 2010       Mathijs Miermans
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace HexTD
{
	public class TowerGreenTwo : TowerBase
	{

		public static int price = 7000;
		public static int damage = 500;
		public static float range = 4.5f;

		private int[] fireAt = { -1, -1 };
		private float[] enemyX = { 0, 0 };
		private float[] enemyY = { 0, 0 };
		private EnemySprite[] enemy = new EnemySprite[2];
		private float rJump = 2.5f;
		private COLOR damageColor = COLOR.GREEN;
		private Color lineColorDark;

		/** Creates a new instance of TowerGreenTwo */
		public TowerGreenTwo(Game game, Context context, Hex hex)
			: base(game, TowerFactory.type.greenTwo, price, damage, range)
		{
			this.lineColor = Color.Green;
			this.lineColorDark = new Color(255, (byte)(this.lineColor.R / 2), (byte)(this.lineColor.G / 2), (byte)(this.lineColor.B / 2));
			this.doInit(context, hex);
		}

		public override void doTick(int gameTime)
		{
			this.fireAt[1] = -1;
			this.fireAt[0] = this.findEnemy(this.fireAt[0], false);
			if (this.fireAt[0] >= 0)
			{
				this.enemy[0] = this.context.enemies[this.fireAt[0]];
				this.enemyX[0] = this.enemy[0].getX();
				this.enemyY[0] = this.enemy[0].getY();
				this.enemy[0].doDamage(this.damageCurrent, this.damageColor);
				this.fireAt[1] = this.findEnemyClosest(this.enemyX[0], this.enemyY[0], this.rJump, this.fireAt);
				if (this.fireAt[1] >= 0)
				{
					this.enemy[1] = this.context.enemies[this.fireAt[1]];
					this.enemyX[1] = this.enemy[1].getX();
					this.enemyY[1] = this.enemy[1].getY();
					this.enemy[1].doDamage(this.damageCurrent, this.damageColor);
				}
			}
		}

		public override string getStatusString()
		{
			string retString = "";
			retString += "<h1>Green Tower 2</h1>" +
					"<p>" +
					"<b>Level:</b> " + (1 + this.level) + "<br>\n" +
					"<b>Damage:</b> " + this.damageText + "<br>\n" +
					"<b>Range:</b> " + this.rangeText + "<br>" +
					"</p>\n";
			return retString;
		}

		public override string getInfoString()
		{
			string retString = "";
			retString += "<h1>Green Tower 2</h1>" +
					"<p>" +
					"Tower with one medium laser that refracts from the first target to a second. Both targets receive full damage.<br>" +
					"Strong against green enemies, very weak against red enemies.</p>" +
					"<p>" +
					"<b>Price:</b> " + price + "<br>" +
					"<b>Damage:</b> " + this.damageCurrent + "<br>" +
					"<b>Range:</b> " + rangeCurrent + "<br>" +
					"<b>Refire rate:</b> 20/s" +
					"</p>";
			return retString;
		}

		protected override void calcDamageRange()
		{
			base.calcDamageRange();
		}

		protected override void generateShapes()
		{
#if PORT
			float width = this.context.hexPaintWidth;
			int shapeCount = 6;
			this.shapes = new Shape[shapeCount];
			this.shapes[0] = new Ellipse.Float(-0.5f*this.paintScale, -0.5f*this.paintScale, 1.0f*this.paintScale, 1.0f*this.paintScale);
			this.shapes[1] = new Ellipse.Float(-0.25f*this.paintScale, -0.25f*this.paintScale, 0.5f*this.paintScale, 0.5f*this.paintScale);
			this.shapes[2] = new LineSegment.Float(0, 0.5f*paintScale, 0, paintScale);
			this.shapes[3] = new LineSegment.Float(-0.5f*width, -0.5f*0.5f*paintScale, -width, -0.5f*paintScale);
			this.shapes[4] = new LineSegment.Float( 0.5f*width, -0.5f*0.5f*paintScale,  width, -0.5f*paintScale);
			this.shapes[5] = new Ellipse.Float(-1.0f*this.context.strokeScale, -1.0f*this.context.strokeScale, 2.0f*this.context.strokeScale, 2.0f*this.context.strokeScale);
        
			this.shapeAction = new int[shapeCount];
			this.shapeAction[0] = 1;
			this.shapeAction[1] = 1;
			this.shapeAction[2] = 1;
			this.shapeAction[3] = 1;
			this.shapeAction[4] = 1;
			this.shapeAction[5] = 1;
        
			this.shapeColor = new Color[shapeCount];
			this.shapeColor[0] = this.lineColor;
			this.shapeColor[1] = this.lineColorDark;
			this.shapeColor[2] = this.lineColor;
			this.shapeColor[3] = this.lineColor;
			this.shapeColor[4] = this.lineColor;
			this.shapeColor[5] = this.lineColor;
        
			this.strokes = new Brush[shapeCount];
			this.strokes[0] = new Brush(2.0f*this.context.strokeScale, Brush.CAP_ROUND, Brush.JOIN_BEVEL);
			this.strokes[1] = this.strokes[0];
			this.strokes[2] = this.strokes[0];
			this.strokes[3] = this.strokes[0];
			this.strokes[4] = this.strokes[0];
			this.strokes[5] = this.strokes[0];
#endif
		}

		public override void DrawEffects(GameTime gameTime)
		{
#if PORT
			Brush defaultStroke = g2.getStroke();
        
			if (this.fireAt[0] > -1) {
				g2.setColor(this.lineColor);
				float e1x = this.enemy[0].getPaintX() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
				float e1y = this.enemy[0].getPaintY() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
				g2.draw(new LineSegment.Float(this.xPaint, this.yPaint, e1x, e1y));
				if (this.fireAt[1] > -1) {
					float e2x = this.enemy[1].getPaintX() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
					float e2y = this.enemy[1].getPaintY() + ( (float)(Math.random()-0.5)*this.paintScale/10f );
					g2.draw(new LineSegment.Float(e2x, e2y, e1x, e1y));
				}
			}
        
			g2.setStroke(defaultStroke);
#endif
		}
	}
}